//
//  HJModifyIntroduceController.m
//  KaiChat
//
//  Created by lx on 2023/12/15.
//

#import "HJModifyIntroduceController.h"
#import "QMUITextView.h"
#import "HJIM.h"
#import "HJMinePresent.h"
#import "HJUserModel.h"

@interface HJModifyIntroduceController ()

@property (nonatomic, assign) NSInteger maxLenght;
@property (nonatomic, weak) UILabel *lengthLabel;
@property (nonatomic, weak) QMUITextView *textView;
@property (nonatomic, weak) HJBarButton *doneButton;

@end

@implementation HJModifyIntroduceController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.navigationBar.title = @"What’s up";
    self.navigationBar.lineView.hidden = YES;
    
    _maxLenght = 128;

    HJBarButton *doneButton = [HJBarButton barButtonWithTitle:@"Done" color:UIColor.tj_mainColor target:self action:@selector(didClickDoneButton)];
    [doneButton setTitleColor:UIColor.tj_BEBDCAColor forState:UIControlStateDisabled];
    doneButton.enabled = NO;
    [self.navigationBar.contentView addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.width.offset(40);
        make.centerY.offset(0);
        make.height.offset(40);
    }];
    _doneButton = doneButton;
    
    UIView *textContentView = [UIView addToView:self.view];
    textContentView.addCornerRadius(10);
    textContentView.backgroundColor = UIColor.tj_F6F6F6Color;
    [textContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.offset(20 + KNavBarHight);
        make.height.offset(160);
    }];
    
    
    QMUITextView *textView = [QMUITextView addToView:textContentView];
    textView.text = _whatsUp;
    textView.font = UIFontMake(14);
    textView.tintColor = UIColor.tj_mainColor;
    textView.placeholderColor = UIColor.tj_placeholderTextColor;
    textView.placeholder = @"Briefly introduce yourself…";
    textView.backgroundColor = UIColor.tj_F6F6F6Color;
    textView.maximumTextLength = _maxLenght;
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(8);
        make.bottom.inset(35);
        make.right.inset(10);
    }];
    
    UILabel *lengthLabel = [UILabel lableWithSuperView:textContentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    [lengthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(15);
        make.bottom.inset(0);
        make.height.offset(35);
    }];
    
    _lengthLabel = lengthLabel;
    _textView = textView;
        
    [self updatalengthLabel];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextChanged:) name:UITextViewTextDidChangeNotification object:textView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(500 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
        [self.textView becomeFirstResponder];
    });
    
}

- (void)handleTextChanged:(NSNotification *)not {
    if (not.object != _textView) return;
    
    self.doneButton.enabled = _textView.text.length;
    [self updatalengthLabel];
}

- (void)updatalengthLabel {
    if ( _textView.text.length > 0) {
        _lengthLabel.attributedText = [[NSString stringWithFormat:@"%ld/%ld", _textView.text.length, _maxLenght] attributedStringWithRangeString:[NSString stringWithFormat:@"%ld", _textView.text.length] rangeColor:UIColor.tj_333333Color color:UIColor.tj_BEBDCAColor];
    } else {
        _lengthLabel.attributedText = [[NSString stringWithFormat:@"%ld/%ld", _textView.text.length, _maxLenght] attributedStringWithRangeString:[NSString stringWithFormat:@"%ld", _textView.text.length] rangeColor:UIColor.tj_BEBDCAColor color:UIColor.tj_BEBDCAColor];
    }
}

- (void)didClickDoneButton {
    
    [HJHUD showLoadingInView:self.view text:nil];
    
    NSString *whatsUp = _textView.text;
    [HJMinePresent modifyWhatsUp:whatsUp complete:^(NSError * _Nullable error) {
        if (error) {
            [HJHUD showErrorMessage:@"Operation failed"];
            return;
        }
        
        if (self.complete) {
            self.complete(whatsUp);
        }
        
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        userModel.whatsUp = whatsUp;
        [userModel bg_saveOrUpdateAsync:nil];
        
        [self.textView resignFirstResponder];
        [HJHUD showSucceedMessage:@"Successful operation"];
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:_textView];
}




@end
